<template>
  <div class="wrapper">
    <div class="box">
      <div class="logo">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <div class="form">
        <el-form ref="form" :model="model" :rules="rules">
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-s-custom"
              v-model="model.username"
              :maxlength="10"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              type="password"
              v-model="model.password"
              :maxlength="15"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="buttons">
          <el-button type="primary" :loading="loading" @click="userLogin"
            >登录</el-button
          >
          <el-button type="info" @click="reset">重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { login } from "api/user";
import { getMenus } from "api/rights";
export default {
  name: "login",
  data() {
    return {
      /**
       * 登录loading
       */
      loading: false,
      /**
       * 登录信息
       */
      model: {},
      /**
       * 规则
       */
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
          },
          {
            min: 3,
            max: 10,
            message: "用户名长度为3到10个字符",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
          },
          {
            min: 6,
            max: 15,
            message: "密码长度为6到15个字符",
          },
        ],
      },
    };
  },
  methods: {
    /**
     * 用户登录
     */
    async userLogin() {
      try {
        /**
         * 成功
         */
        await this.$refs.form.validate();
        this.loading = true;
        const data = await login(this.model);
        this.$store.commit("setUser", data);
        const menus = await getMenus();
        this.$store.commit("setMenus", menus);
        this.$router.push({ name: "HomeLayout" });
      } catch (error) {
        /**
         * 打印错误的
         */
      }
      this.loading = false;
    },
    /**
     * 重置
     */
    reset() {
      this.$refs.form.resetFields();
      this.model = {};
    },
  },
};
</script>
<style scoped lang="scss">
.wrapper {
  height: 100%;
  background: var(--mainBackColor);
  position: relative;

  .box {
    width: 450px;
    height: 360px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -225px;
    margin-top: -180px;

    .buttons {
      position: absolute;
      right: 20px;
    }

    .form {
      box-sizing: border-box;
      width: 100%;
      position: absolute;
      bottom: 100px;
      padding: 0 20px;
    }

    .logo {
      width: 130px;
      height: 130px;
      position: absolute;
      left: 50%;
      top: 0;
      border-radius: 50%;
      background-color: white;
      transform: translate(-50%, -50%);
      border: 11px solid #fff;
      box-shadow: 0 0 10px #ddd;
      overflow: hidden;

      img {
        width: 100%;
        height: 100%;
        background-color: #eee;
      }
    }
  }
}
</style>